/*
 * 文件名：FlowPapayResponceHandler.java	 
 * 时     间：下午7:27:48
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.message.handler.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;

import com.cloud.threads.UpDateCouponCashRunble;
import com.cz073586.common.billRules.utils.DateUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.CarParkOutMapper;
import com.local.model.CarParkIn;
import com.local.model.CarParkOut;
import com.local.model.Crossing;
import com.local.model.LocalCouponCash;
import com.local.model.LocalPayBill;
import com.local.model.LocalPayBillCopy;
import com.local.model.LocalStation;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.CrossingService;
import com.local.service.face.LocalCouponCashService;
import com.local.service.face.LogService;
import com.local.service.face.ParkwellCarOutService;
import com.local.service.face.ParkwellPayBillService;
import com.local.util.MyDateUtil;
import com.message.entity.TransDataVo;
import com.message.handler.face.ResponceHandler;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:云平台支付成功后生成流水.
 * 
 * @ClassName: FlowPapayResponceHandler
 * @author Administrator
 * @date 2015年10月13日 下午7:27:48
 */
public class FlowPapayResponceHandler implements ResponceHandler
{
	
	@SuppressWarnings("rawtypes")
	@Override
	public TransDataVo resoponse(ApplicationContext srpingApp,
			TransDataVo transData)
	{
		TransDataVo<LocalPayBill> result = new TransDataVo<LocalPayBill>();
		
		try
		{
			if (!Tools.isEmpty(transData.getData()))
			{
				// 移动支付后插入移动支付流水
				
				result.setResult(CommonContans.STRUTS_SUCCESS);
				String json = JsonUtil.getJsonString(transData.getData());
				LocalPayBill intertion = JsonUtil.fromJson(json,
						LocalPayBill.class);
				ParkwellPayBillService pbiil = srpingApp
						.getBean(ParkwellPayBillService.class);
				
				// 2015-11-25 插入流水之前判断本地系统的进场ID是否在本地存在，矫正已本地为准
				if (Tools.isEmpty(intertion.getParkInUuid()))
				{
					result.setErrorCode("100002-200003-600001");
					result.setErrorMsg("找不到进场标记，支付失败.");
					throw new BaseException("支付失败");
				}
				
				CarParkIn carInfo = srpingApp.getBean(CarParkInMapper.class)
						.selectCarNo(intertion.getCarNo());
				if (!Tools.isEmpty(carInfo))
				{
					intertion.setCarinCreatAt(!Tools.isEmpty(carInfo
							.getCreateTime()) ? carInfo.getCreateTime() : null);
				}
				// 支付的车辆进场uuid 与本地系统在场uuid不匹配时，矫正为本地系统当前在场ID，防止重复数据
				if (!Tools.isEmpty(carInfo)
						&& !intertion.getParkInUuid().equalsIgnoreCase(
								carInfo.getUuid()))
				{
					if (!Tools.isEmpty(carInfo.getOpenTime())
							&& carInfo.getOpenTime().contains("-"))
					{
						intertion.setCarinCreatAt(DateUtil
								.getDateByString(carInfo.getOpenTime()));
					} else
					{
						intertion.setCarinCreatAt(carInfo.getCreateTime());
					}
					intertion.setParkInUuid(carInfo.getUuid());
				}
				// end 2015-11-25
				
				// 2015-12-10 添加进场出场信息
				CarParkOut carOutInfo1 = srpingApp.getBean(
						CarParkOutMapper.class).selectCarNo(
						intertion.getCarNo());
				if (!Tools.isEmpty(carOutInfo1))
				{
					intertion.setCarOutUuid(carOutInfo1.getUuid());
					intertion.setCaroutCareteAt(carOutInfo1.getCreateTime());
					intertion.setCarinCreatAt(carInfo.getCreateTime());
				}
				if (!Tools.isEmpty(intertion.getCarinCreatAt())
						&& !Tools.isEmpty(intertion.getCaroutCareteAt()))
				{
					// 停车时长
					String stayTimeLong = MyDateUtil.betweenTimeString(
							intertion.getCaroutCareteAt(),
							intertion.getCarinCreatAt());
					intertion.setStayTimeLong(stayTimeLong);
				}
				// 2015-12-10 end
				// 判断重复插入
				LocalPayBillCopy pb=new LocalPayBillCopy();
				BeanUtils.copyProperties(intertion, pb);
				pbiil.savePayBillCopy(pb);
				/*pbiil.saveParkwellPayBill(intertion);*/
				
				// 抵用劵service
				LocalCouponCashService localCouponCashService = srpingApp
						.getBean(LocalCouponCashService.class);
				
				LogService logService = srpingApp.getBean(LogService.class);
				
				// 查抵用劵
				List<LocalCouponCash> li = localCouponCashService
						.queryCouponCashByCarNo(carInfo.getCarNo());
				if (!Tools.isEmpty(li) && li.size() > 0)
				{
					// 生成流水
					couponCashPayBill(li, intertion.getParkInUuid(), srpingApp);
					// 通知周光伟
					// 使用的商家抵用劵记录，上传到云系统，并修改使用状态
					for (LocalCouponCash lc : li)
					{
						// 云端调用第三方，通知接口，告知第三方(周光伟.已经使用过)
						UpDateCouponCashRunble upload = new UpDateCouponCashRunble(
								lc, logService);
						ThreadPoolUtil.instance().execute(upload);
					}
				}
				// 判断车辆是否在岗亭下,如果时则开闸
				if (!Tools.isEmpty(intertion.getCarNo()))
				{
					CarParkOut carOutInfo = srpingApp.getBean(
							CarParkOutMapper.class).selectCarNo(
							intertion.getCarNo());
					if (!Tools.isEmpty(carOutInfo)
							&& carOutInfo.getStatus() == 1
							&& !Tools.isEmpty(carOutInfo.getEquipment()))
					{
						// 车辆再岗亭下面,执行开闸、同步到云、更新状态为0
						srpingApp.getBean(CarInAndOutService.class)
								.addCarParkOutBill(carOutInfo);
						// 开闸
						CrossingService crosingService = srpingApp
								.getBean(CrossingService.class);
						// List<LocalEquipment> listResout =
						// crosingService.queryCrossingByEquimentNo(carOutInfo.getEquipment())
						List<Crossing> crossingList = crosingService
								.queryCrossingByEquimentNo(carOutInfo
										.getEquipment());
						if (!crossingList.isEmpty())
						{
							
							// 确定路口属于哪个岗亭
							List<LocalStation> listStaions = crosingService
									.queryStaionsByCrosList(crossingList);
							crosingService.openGATE(crossingList.get(0)
									.getUuid(), listStaions.isEmpty() ? null
									: listStaions.get(0).getStationUuid());
							// 如果开了闸更新状态
							ParkwellCarOutService carOutService = srpingApp
									.getBean(ParkwellCarOutService.class);
							carOutService.updataForCarOut(carOutInfo.getUuid(),
									intertion.getParkInUuid());
						}
					}//
				}
			}
		} catch (Exception e)
		{
			result.setResult(CommonContans.STRUTS_ERROR);
		}
		
		return result;
	}
	
	public void couponCashPayBill(List<LocalCouponCash> li, String carInUuid,
			ApplicationContext srpingApp)
	{
		// 流水表 service
		ParkwellPayBillService parkwellPayBillService = srpingApp
				.getBean(ParkwellPayBillService.class);
		
		// 抵用劵service
		LocalCouponCashService localCouponCashService = srpingApp
				.getBean(LocalCouponCashService.class);
		
		// 日志service
		LogService logService = srpingApp.getBean(LogService.class);
		
		CarParkIn carInfo = srpingApp.getBean(CarParkInMapper.class)
				.selectByPrimaryKey(carInUuid);
		
		if (!Tools.isEmpty(li))
		{
			for (LocalCouponCash lc : li)
			{
				LocalPayBillCopy parkwellPayBill = new LocalPayBillCopy();
				parkwellPayBill.setPayBillUuid(UUIDGenerateUtil.generate());
				parkwellPayBill.setCarNo(lc.getCarNo());
				parkwellPayBill
						.setPayTime(new Date(System.currentTimeMillis()));
				parkwellPayBill.setPayType("抵用劵");
				parkwellPayBill.setPrice(lc.getPrice().doubleValue());
				parkwellPayBill.setActualPrice(lc.getPrice().doubleValue());
				parkwellPayBill.setParkInUuid(carInUuid);
				if (!Tools.isEmpty(carInfo))
				{
					parkwellPayBill.setCarinCreatAt(!Tools.isEmpty(carInfo
							.getCreateTime()) ? carInfo.getCreateTime() : null);
				}
				
				parkwellPayBillService.savePayBillCopy(parkwellPayBill);
				LocalPayBill pb=new LocalPayBill();
				BeanUtils.copyProperties(parkwellPayBill,pb);
				/*parkwellPayBillService.insertSelective(parkwellPayBill);*/
				/*// 默认使用商家抵用劵，流水上传到云端
				if (CloudConfigManager.instance().isOpen())
				{
					UploadPayBillRunble upload = new UploadPayBillRunble(
							pb, logService);
					ThreadPoolUtil.instance().execute(upload);
				}*/
				// 删除本地系统的，商家抵用劵情况
				localCouponCashService.deleteByPrimaryKey(lc.getCouponId());
			}
		}
	}
}
